﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#include"Texture2D.h"
#include "../external/WICTextureLoader/WICTextureLoader.h"
#include "../utility/MacroTool.h"
#include "../utility/utility.h"
#include "../base/VertexType.h"
#include "../utility/StringUtility.h"
#include "../render/TextureCache.h"
#include "../base/GameWorld.h"
#include <string>
#include "../render/IDirectRender.h"
USING_NS_DX;

namespace SkyEngine2d
{

	Texture2D::Texture2D()
		:m_resource(nullptr)
		, m_textureView(nullptr)
	{
	}


	Texture2DSharedPtr Texture2D::create(const std::wstring& file_name)
	{
		auto p = GameWorld::getInstance()->getTextureCache()->addImage(file_name);
		return p;
	}

	Texture2D::~Texture2D()
	{

	}

	bool Texture2D::initWithFile(const std::wstring& file_name)
	{
		auto d3dDevice_ = GameWorld::getInstance()->getDirectInterface()->getD3dDevice();

		auto hr = CreateWICTextureFromFile(d3dDevice_, file_name.c_str(), m_resource.ReleaseAndGetAddressOf(), m_textureView.ReleaseAndGetAddressOf());
		if (FAILED(hr))
		{
			return false;
		}

		D3D11_TEXTURE2D_DESC colorTexDesc;
		((ID3D11Texture2D*)(m_resource.Get()))->GetDesc(&colorTexDesc);

		m_contentSize.set((float)colorTexDesc.Width, (float)colorTexDesc.Height);

		return true;
	}

	const Size& Texture2D::getContentSize() const
	{
		return m_contentSize;
	}


	D3dShaderResourceViewComPtr Texture2D::getD3dShaderResourceView()
	{
		return m_textureView;
	}

	D3dResourceComPtr Texture2D::getD3dResourceComPtr()
	{
		return m_resource;
	}

}
